/**
 * 
 */

package com.ekingstar.framework.util;

import java.util.List;

import com.ekingstar.framework.domain.Function;

/**
 * 
 *
 */
@SuppressWarnings("unchecked")
public class MenuUtil {

	public static String getExtjsMenu(List<Function> functions){
		StringBuffer sb = new StringBuffer();
		sb.append("var tb = new Ext.Toolbar(); tb.render('user_menu'); var pm = null;var cm = null;");
		if (functions == null || functions.isEmpty()){
			sb.append("tb.add('<a href=/j_spring_security_logout>ע��</a>');");
			return sb.toString();}
		for (int i = 0 ;i < functions.size() ; i++){
			Function func = functions.get(i);
			String name = func.getName();
			List<Function> children = func.getChilden();
			if (children != null && children.size() > 0 ){
				sb.append(" pm = new Ext.menu.Menu(); tb.add({text:'"+name+"',menu:pm});");
			}else {
				sb.append("tb.add('<a href="+func.getAddresss()+">"+name+"</a>');");
			}
			getExtjsSubMenu(func,sb);
		}
		sb.append("tb.add('<a href=/j_spring_security_logout>ע��</a>');");
		return sb.toString();
	}
	
	public static void getExtjsSubMenu(Function fun,StringBuffer sb){
		if (fun == null || sb == null) return ;
		List<Function> children = fun.getChilden();
		if (children == null || children.isEmpty()) return;
		for (int i = 0; i < children.size();i++ ){
			Function cfun = children.get(i);
			List<Function> cchildren = cfun.getChilden();
			if (cchildren == null || cchildren.isEmpty()){
				//Ҷ�ӽڵ�
				sb.append("pm.add({text:'"+cfun.getName()+"',href:'"+cfun.getAddresss()+"'});");
			}else {
				sb.append("cm = new Ext.menu.Menu();pm.add({text:'"+cfun.getName()+"',menu:cm});pm = cm;cm = null;");
				getExtjsSubMenu(cfun,sb);
			}
		}
	}
	
}
